package drinkkiosk.buttons;

import serialtool.SerialTool;
import drinkkiosk.CardPanel;
import drinkkiosk.cards.SendCard;
import drinkkiosk.cards.UnlockCard;
import drinkkiosk.drinks.SpecialDrink;
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.SwingWorker;
import networktool.KioskClient;

/**EnterButton is used by both the SendCard and the UnlockCard. If pressed while
 * the SendCard is displayed the TableNumber entered will be added to the Drink.
 * A confirmation Dialog will pop-up and the Patron will press a Big Red Button
 * connected to the embedded board. When that button is pressed the Drink will
 * be sent to the BartenderQueue
 *
 * @author Charles Ritchea
 */
public class EnterButton extends JButton implements ActionListener {

    /**EnterButton() this constructor sets the button text to Enter and
     * attaches an ActionListener
     */
    public EnterButton() {
        super("Enter");
        setFont(new java.awt.Font("Arial", 0, 48));
        addActionListener(this);
    }

    /**Determines the context and either unlocks the manager card, or 
     * shows the confirmation dialog.
     * @param e ActionEvent the event of pressing the EnterButton
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        JButton button = (JButton) e.getSource();
        JPanel numPanel = (JPanel) button.getParent();
        JLayeredPane layeredPane = (JLayeredPane) numPanel.getParent();
        JPanel card = (JPanel) layeredPane.getParent();
        if (card instanceof SendCard) {
            SendCard sendCard = (SendCard) card;
            final CardPanel cardPanel = (CardPanel) sendCard.getParent();
            final SpecialDrink special = cardPanel.getSpecialsCard().getSpecial();
            special.setTableNumber(sendCard.getTableNumber());
           
            final CardLayout cardLayout = (CardLayout) cardPanel.getLayout();
            cardLayout.show(cardPanel, "confirmation");

            //exp
            
            SwingWorker worker = new SwingWorker() {
                
                @Override
                protected Object doInBackground() throws Exception {
                    SerialTool serialTool = new SerialTool();
                    String test = "o_o";
                    try{
                    while (test.contentEquals("o_o")) {
                        test += serialTool.getResult("");
                        System.out.println(test);
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException ex) {
                            Logger.getLogger(EnterButton.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        
                    }
                    test = "o_o";
                    }                 catch (java.lang.Throwable t) {
                }
                     try {
                KioskClient.sendDrink(special);
            } catch (IOException ex) {
                Logger.getLogger(EnterButton.class.getName()).log(Level.SEVERE, null, ex);
            }
                    cardLayout.show(cardPanel, "welcome");
                    throw new UnsupportedOperationException("Not supported yet.");
                }
            };






            worker.execute();
            //end exp

        } else if (card instanceof UnlockCard) {
            UnlockCard unlockCard = (UnlockCard) card;
            unlockCard.unlock(e);
        }
    }
}
